package com.xms.dependency.service;

import com.xms.core.module.ModuleCollection;
import com.xms.dependency.XmsDependencyException;
import com.xms.dependency.model.DependentDescriptor;
import com.xms.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * DependencyChecker
 * </p>
 *
 * @author migo
 * @since 2021/10/23
 */
@Service
public class DependencyChecker implements IDependencyChecker {
    private IDependencyLookupFactory _dependencyFinderFactory;

    @Autowired
    public void DependencyChecker(IDependencyLookupFactory dependencyFinderFactory) {
        _dependencyFinderFactory = dependencyFinderFactory;
    }

    @Override
    public void checkAndThrow(int requiredComponentType, UUID requiredId) throws XmsDependencyException {
        List<DependentDescriptor> dependentComponents = _dependencyFinderFactory.getDependents(requiredComponentType, requiredId);
        if (CollectionUtil.notEmpty(dependentComponents)) {
            throw new XmsDependencyException("对象已被引用", dependentComponents);
        }
    }

    @Override
    public void checkAndThrow(String requiredComponentName, UUID requiredId) throws XmsDependencyException {
        this.checkAndThrow(ModuleCollection.getIdentity(requiredComponentName), requiredId);
    }
}
